package com.wxx.service.tool.oss;


import com.aliyun.oss.OSSClient;
import com.google.common.collect.Maps;
import com.wxx.common.Const;
import com.wxx.util.AppUtil;
import com.wxx.util.PropertiesUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class OssServiceImpl implements OssService {

    private static final String accessKeyId = PropertiesUtil.getProperty("OSS_ACCESS_KEYID");
    private static final String accessKeySecret = PropertiesUtil.getProperty("OSS_ACCESS_KEYSECRET");
    private static final String bucket = PropertiesUtil.getProperty("OSS_BUCKET");
    private static final String ossUrl = PropertiesUtil.getProperty("OSS_URL");

    @Resource
    private OssTransCodeService transCodeService;

    private OSSClient getOSS() {
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
        // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
        // 创建OSSClient实例。
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        return ossClient;
    }

    @Override
    public Map upload(MultipartFile file) throws IOException {

        OSSClient ossClient = getOSS();
        // 上传图片
        String name = file.getOriginalFilename();
        String suffix = name.substring(name.lastIndexOf("."));
        String newName = AppUtil.uuid() + suffix;

        ossClient.putObject(bucket, newName,file.getInputStream());
        // 关闭OSSClient。
        ossClient.shutdown();

        //获取上传地址
//        Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);
//        String url = ossClient.generatePresignedUrl(bucket, newName, expiration).toString();
//        System.out.println("oss绝对路径 >>>>>>>>>>>>>>> "+url);
//        System.out.println("oss相对地址 >>>>>>>>>>>>>>> "+newName);


        /* 转码之后的文件名*/
        String codeName = "";
        //如果上传的视频，进行转码
        if (".MP4".equals(suffix.toUpperCase())  || ".MOV".equals(suffix.toUpperCase()) ||
                ".MPEG".equals(suffix.toUpperCase()) || ".AVI".equals(suffix.toUpperCase()) ||
                ".ASF".equals(suffix.toUpperCase())  || ".WMV".equals(suffix.toUpperCase()) ||
                ".NAVI".equals(suffix.toUpperCase()) || ".3GP".equals(suffix.toUpperCase()) ||
                ".FLV".equals(suffix.toUpperCase())  || ".F4V".equals(suffix.toUpperCase()) ||
                ".RMVB".equals(suffix.toUpperCase()) || ".RM".equals(suffix.toUpperCase())
        ){
            try {
                codeName = AppUtil.uuid()+suffix;
                transCodeService.simpleTranscode(newName,codeName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        Map map = Maps.newHashMap();
        map.put("code", Const.SUCCESS_CODE);
        //传递相对路径
        //codeName是空 上传的图片，不为空是视频
        if ("".equals(codeName)){
            map.put("url", newName);
        }else{
            map.put("url", codeName);
        }
        System.out.println("OSS上传文件返回数据 " + map.toString());
        return map;
    }

    @Override
    public Map uploadBatch(MultipartFile[] files) throws IOException {
        OSSClient ossClient = getOSS();

        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < files.length; i++) {
            MultipartFile file = files[i];
            // 上传图片
            String name = file.getOriginalFilename();
            String newName = AppUtil.uuid() + name.substring(name.lastIndexOf("."));

            ossClient.putObject(bucket, newName, file.getInputStream());

            //获取上传地址
//            Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);
//            String url = ossClient.generatePresignedUrl(StaticConfig.OSS_BUCKET, newName, expiration).toString();
//            System.out.println("oss >>>>>>>>>>>>>>> " + url);

            System.out.println("oss相对地址 >>>>>>>>>>>>>>> " + newName);
            sb.append(newName).append(",");
        }
        // 关闭OSSClient。
        ossClient.shutdown();
        Map map = Maps.newHashMap();
        map.put("code", Const.SUCCESS_CODE);
        //传递相对路径 逗号拼接的文件地址
        map.put("url", sb);
        System.out.println("OSS " + map.toString());
        return map;
    }
}
